package config;

import entity.SysAccounts;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import service.*;

import java.util.Set;

/**
 * @Description:
 * @Author: liujh
 * @Date:
 * @UpdateUser: liujh
 * @Date: 2020/08/10$ 15:18$
 * @UpdateRemark:
 * @Version: 1.0
 */

@Component
@Slf4j
public class UserRealm extends AuthorizingRealm {



    
    @Autowired
    private SysAccountsService sysAccountsService;

    @Autowired
    private SysRolesService sysRolesService;

    @Autowired
    private SysPermissionService sysPermissionService;
    /**
     * Retrieves the AuthorizationInfo for the given principals from the underlying data store.  When returning
     * an instance from this method, you might want to consider using an instance of
     * {@link SimpleAuthorizationInfo SimpleAuthorizationInfo}, as it is suitable in most cases.
     *
     * @param principals the primary identifying principals of the AuthorizationInfo that should be retrieved.
     *
     * @return the AuthorizationInfo associated with this principals.
     *
     * @see SimpleAuthorizationInfo
     */
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        if (null ==principals){
            throw new  AuthenticationException("principals 不能为null");
        }
        log.info("授权开始=========>");
        SysAccounts accounts = (SysAccounts) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> roles =  sysRolesService.queryRoleSet(accounts.getUserId());
        Set<String> perSet = sysPermissionService.queryPerm(accounts.getUserId());
        info.setRoles(roles);
        info.setStringPermissions(perSet);
        log.info("授权完成==============>");
        return info;
    }

    /**
     * Retrieves authentication data from an implementation-specific datasource (RDBMS, LDAP, etc) for the given
     * authentication token.
     * <p/>
     * For most datasources, this means just 'pulling' authentication data for an associated subject/user and nothing
     * more and letting Shiro do the rest.  But in some systems, this method could actually perform EIS specific
     * log-in logic in addition to just retrieving data - it is up to the Realm implementation.
     * <p/>
     * A {@code null} return value means that no account could be associated with the specified token.
     *
     * @param token the authentication token containing the user's principal and credentials.
     *
     * @return an {@link AuthenticationInfo} object containing account data resulting from the
     * authentication ONLY if the lookup is successful (i.e. account exists and is valid, etc.)
     *
     * @throws AuthenticationException if there is an error acquiring data or performing
     *                                 realm-specific authentication logic for the specified <tt>token</tt>
     */
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        SysAccounts accounts = sysAccountsService.queryByAccount(username);
        if (null == accounts){
            throw new UnknownAccountException();
        }
        log.info("account:{}",accounts.getAccount());
        SimpleAuthenticationInfo authorizationInfo = new SimpleAuthenticationInfo(accounts,
                                                        accounts.getPassword(),
                                                        ByteSource.Util.bytes(accounts.getCredentialsSalt()),getName());
        return authorizationInfo;
    }
}
